/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 12.09.12
 * Time: 16:29
 * To change this template use File | Settings | File Templates.
 */
package fgui.control {
import fgui.GuiComponent;
import fgui.IAnimationComponent;

import fgui.UIGraphicsComponent;

import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;

import mx.collections.ArrayCollection;
import mx.core.UIComponent;

/**
 *
 *
 * @see com.fgui.control.DropDownChoice
 */
public class DropDownChoice extends GuiComponent implements IAnimationComponent {
    private var children:ArrayCollection;
    private var titleComponent:TextButton;
    private var maskOfComponent:UIGraphicsComponent;
    private var subMaskForUnExpand:UIGraphicsComponent;

    // for expanding
    private var isExpanded:Boolean = false;

    public function DropDownChoice(coordinate:Point, size:Point, title:String) {
        super(coordinate, size);

        renderColor = 0xffffff;
        renderAlpha = 0.1;

        subMaskForUnExpand = new UIGraphicsComponent();
        addChild(subMaskForUnExpand);
        //subMaskForUnExpand.drawRoundedRectangle(renderColor, renderAlpha, new Point(-10, -margin), new Point(size.x + 2*margin, size.y + 2*margin), new Point(20, 20));
        subMaskForUnExpand.drawRoundedRectangle(renderColor, 1, new Point(-10, -10), new Point(300, 300), new Point(20, 20));

        titleComponent = new TextButton(new Point(0, 0), size, title);
        titleComponent.renderColor = 0xaaaaaa;
        titleComponent.renderDefault();
        addChild(titleComponent);
        maskOfComponent = new UIGraphicsComponent();
        maskOfComponent.drawRoundedRectangle(0x000000, 1, new Point(-margin, 0), new Point(size.x + 2 * margin, size.y), new Point(20, 20));
        maskOfComponent.width = size.x;
        maskOfComponent.height = size.y;
        addChild(maskOfComponent);
        this.mask = maskOfComponent;

        children = new ArrayCollection();

        addEventListener(MouseEvent.MOUSE_OVER, renderOnMouseOver);
        titleComponent.addEventListener(MouseEvent.MOUSE_DOWN, renderOnMouseDown);
        addEventListener(MouseEvent.MOUSE_UP, renderOnMouseOver);
        subMaskForUnExpand.addEventListener(MouseEvent.MOUSE_OUT, renderOnMouseOut);
    }

    public function renderOnMouseOver(event:MouseEvent):void {
    }

    public function renderOnMouseDown(event:MouseEvent):void {
        if (!isExpanded)
            addEventListener(Event.ENTER_FRAME, expandMask);
        else
            addEventListener(Event.ENTER_FRAME, unexpandMask);
    }

    public function renderOnMouseOut(event:MouseEvent):void {
        addEventListener(Event.ENTER_FRAME, unexpandMask);
    }

    public function getAllChoices():ArrayCollection {
        return this.children;
    }

    public function getChoiceById():void {

    }

    private var childrenHeight:int = 30;

    public function addChoice(text:String, callback:Function):void {
        var item:TextButton = new TextButton(
                new Point(0, this.children.length * childrenHeight + bgHeight),
                new Point(this.bgWidth, childrenHeight),
                (this.children.length + 1) + ". " + text
                );
        item.addEventListener(MouseEvent.CLICK, callback);
        item.addEventListener(MouseEvent.CLICK,
                function(event:MouseEvent):void {
                    addEventListener(Event.ENTER_FRAME, unexpandMask);
                });
        this.children.addItem(item);
        addChild(item);
    }

    public function removeAllChoices():void {
        this.children.removeAll();
    }

    private var speedExpandingMask:int = 25;
    private var maskHeight:int = childrenHeight;

    public function expandMask(event:Event):void {
        var summaryHeight:Number = this.getSize().y + childrenHeight * this.children.length;
        if (maskHeight >= summaryHeight) {
            isExpanded = true;
            maxExpand();
            removeEventListener(Event.ENTER_FRAME, expandMask);
        }
        else {
            maskHeight += speedExpandingMask;
            subMaskForUnExpand.drawRoundedRectangle(renderColor, renderAlpha, new Point(-margin, -margin), new Point(this.getSize().x + 2 * margin, maskHeight + 2 * margin), new Point(20, 20));
            maskOfComponent.drawRoundedRectangle(0x000000, 1, new Point(-margin, 0), new Point(this.getSize().x + 2 * margin, maskHeight), new Point(20, 20));
        }
    }

    public function unexpandMask(event:Event):void {
        var summaryHeight:Number = this.getSize().y + childrenHeight * this.children.length;
        if (maskHeight <= this.getSize().y) {
            isExpanded = false;
            removeEventListener(Event.ENTER_FRAME, unexpandMask);
        }
        else {
            maskHeight -= speedExpandingMask;
            subMaskForUnExpand.drawRoundedRectangle(renderColor, renderAlpha, new Point(-margin, -margin), new Point(this.getSize().x + 2 * margin, maskHeight + 2 * margin), new Point(20, 20));
            maskOfComponent.drawRoundedRectangle(0x000000, 1, new Point(-margin, 0), new Point(this.getSize().x + 2 * margin, maskHeight), new Point(20, 20));
        }
    }

    private function maxExpand():void {
        maskOfComponent.drawRoundedRectangle(0x000000, 1, new Point(-margin, -margin), new Point(this.getSize().x + 2 * margin, maskHeight + 2 * margin), new Point(20, 20));
    }
}
}
